package com.app.controller;

import com.app.common.Result;
import com.app.model.EmergentContactModel;
import com.app.model.PopularScienceModel;
import com.app.server.EmergentContactService;
import com.app.utils.CommonHelp;
import com.app.vo.EmergentContactVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 紧急联系
 */
@Slf4j
@RestController
@RequestMapping("/sbbz/emergentContact")
public class EmergentContactController {
    @Autowired
    private EmergentContactService emergentContactService;

    //分页查询紧急联系
    @GetMapping("/getEmergentList")
    public Result getEmergentList(@RequestParam(defaultValue = "0") Integer page,
                                  @RequestParam(defaultValue = "10") Integer limit,String keyword) {
        EmergentContactVo emergentList = emergentContactService.getEmergentList(page, limit,keyword);
        return Result.success(emergentList);
    }

    //根据id查
    @GetMapping("/getEmergentById")
    public Result getEmergentById(Integer id) {
        EmergentContactModel emergent = emergentContactService.getById(id);
        return Result.success(emergent);
    }

    // 新增和修改
    @PostMapping("/addAndUpdateEmergent")
    public Result addAndUpdateEmergent(@RequestBody EmergentContactModel emergentContactModel) {
        Integer id = emergentContactModel.getId();
        EmergentContactModel emergentContact = emergentContactService.getById(emergentContactModel.getId());
        if (emergentContact != null) {
            emergentContactModel.setUpdateTime(CommonHelp.getNowTimestamp());
            emergentContactService.updateById(emergentContactModel);
            return Result.success("修改成功");
        } else {
            emergentContactModel.setCreateTime(CommonHelp.getNowTimestamp());
            emergentContactService.save(emergentContactModel);
            return Result.success("添加成功");
        }
    }

    //删除
    @GetMapping("/delEmergent")
    public Result delEmergent(Integer id) {
        emergentContactService.removeById(id);
        return Result.success("删除成功");
    }

}
